package pers.cj.pdos.controller;


import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.HouseWaitOut;
import pers.cj.pdos.entity.HouseWaitOutGoods;
import pers.cj.pdos.excel.ExcelHouseWaitIn;
import pers.cj.pdos.excel.ExcelHouseWaitOut;
import pers.cj.pdos.service.IHouseWaitInService;
import pers.cj.pdos.service.IHouseWaitOutService;
import pers.cj.pdos.task.CleanTempExcelTask;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.utils.FileProperUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2020-02-21
 */
@Controller
@RequestMapping("/house")
public class HouseWaitOutController extends BaseController {
    @Autowired
    IHouseWaitOutService houseWaitOutService;


    @RequiresPermissions("houseOut:query")
    @RequestMapping("/houseWaitOut")
    public String houseWaitOut(){
        return "/houseOut/houseWaitOut";
    }


    @RequiresPermissions("houseOut:query")
    @RequestMapping("/houseWaitOutDetail")
    public String houseWaitOutDetail(Model model, String id){
        model.addAttribute("id",id);
        return "/houseOut/houseWaitOutDetail";
    }


    @ResponseBody
    @RequiresPermissions("houseOut:query")
    @RequestMapping("/houseWaitOut/getList")
    public ResponseList getList(int page,
                                int limit,
                                String status,
                                String type,
                                String search,
                                String no,
                                String goodsName,
                                String remark,
                                String unitName,
                                String makeTime1,
                                String makeTime2,
                                String makeUserId,
                                String userId,
                                String date1,
                                String date2,
                                String storeId,
                                String houseId){
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<HouseWaitOutVo> list = houseWaitOutService.listVo(company.getId(),
                page,
                limit,
                Utils.genInteger(status),
                Utils.genInteger(type),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(unitName),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                makeUserId,
                PdosUtils.getViewOrderStoreId(userVo,null),
                PdosUtils.getViewOrderUserId(userVo,userId),
                Utils.genDate(date1),
                Utils.genDate(date2),
                storeId,
                houseId);

        long count = houseWaitOutService.count(company.getId(),
                Utils.genInteger(status),
                Utils.genInteger(type),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(unitName),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                makeUserId,
                PdosUtils.getViewOrderStoreId(userVo,null),
                PdosUtils.getViewOrderUserId(userVo,userId),
                Utils.genDate(date1),
                Utils.genDate(date2),
                storeId,
                houseId);

        return ResponseList.success(count,list);
    }

    @ResponseBody
    @RequiresPermissions("houseOut:query")
    @RequestMapping("/houseWaitOut/getVoById")
    public ResponseData getVoById(String id) {
        return ResponseData.success(houseWaitOutService.getVoById(id));
    }

    @ResponseBody
    @RequiresPermissions("houseOut:export")
    @RequestMapping("/houseWaitOut/export")
    public ResponseData export(String status,
                               String type,
                               String search,
                               String no,
                               String goodsName,
                               String remark,
                               String unitName,
                               String makeTime1,
                               String makeTime2,
                               String makeUserId,
                               String userId,
                               String date1,
                               String date2,
                               String storeId,
                               String houseId){
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<HouseWaitOutVo> list = houseWaitOutService.listVo(company.getId(),
                null,
                null,
                Utils.genInteger(status),
                Utils.genInteger(type),
                Utils.genLikeData(search),
                Utils.genLikeData(no),
                Utils.genLikeData(goodsName),
                Utils.genLikeData(remark),
                Utils.genLikeData(unitName),
                Utils.genDate(makeTime1),
                Utils.genDate(makeTime2),
                makeUserId,
                PdosUtils.getViewOrderStoreId(userVo,null),
                PdosUtils.getViewOrderUserId(userVo,userId),
                Utils.genDate(date1),
                Utils.genDate(date2),
                storeId,
                houseId);

        String fileName = "待出库单列表" + DateUtils.getDate(new Date(), DateUtils.YYYYMMDDHHMMSSSS) + ".xlsx";
        try {
            String path = FileProperUtil.getInstance().getVal("excel.temp");
            OutputStream out = new FileOutputStream(path+fileName);
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX,true);
            Sheet sheet1 = new Sheet(1,0, ExcelHouseWaitOut.class);
            sheet1.setSheetName("待出库单信息");
            writer.write(genExcelList(list), sheet1);
            writer.finish();
            out.close();
            CleanTempExcelTask.put(fileName,new Date().getTime());
            return ResponseData.success(fileName);
        } catch (IOException e) {
            return ResponseData.fail("订单列表导出失败");
        }
    }

    private List<ExcelHouseWaitOut> genExcelList(List<HouseWaitOutVo> houseWaitOutVoList) {
        List<ExcelHouseWaitOut> list = new ArrayList<>();
        for (HouseWaitOutVo vo : houseWaitOutVoList){
            ExcelHouseWaitOut excel = new ExcelHouseWaitOut();
            if (vo.getType() == 1){
                excel.setDate(DateUtils.getDate(vo.getInGoodsRefundVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getInGoodsRefundVo().getNo());
                excel.setUnit(vo.getInGoodsRefundVo().getProvider().getName());
                excel.setTypeName("进货退货");
            } else if (vo.getType() == 2){
                excel.setDate(DateUtils.getDate(vo.getSaleVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getSaleVo().getNo());
                excel.setUnit(vo.getSaleVo().getCustom().getName());
                excel.setTypeName("销售");
            } else if (vo.getType() == 3){
                excel.setDate(DateUtils.getDate(vo.getGoodsTransferVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getGoodsTransferVo().getNo());
                excel.setUnit("");
                excel.setTypeName("调拨");
            } else if (vo.getType() == 4){
                excel.setDate(DateUtils.getDate(vo.getBorrowOutVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getBorrowOutVo().getNo());
                excel.setUnit(vo.getBorrowOutVo().getCustom().getName());
                excel.setTypeName("借出");
            } else if (vo.getType() == 5){
                excel.setDate(DateUtils.getDate(vo.getBorrowInBackVo().getDate(), DateUtils.YYYY_MM_DD));
                excel.setNo(vo.getBorrowInBackVo().getNo());
                excel.setUnit(vo.getBorrowInBackVo().getProvider().getName());
                excel.setTypeName("借入归还");
            }

            excel.setStore(vo.getHouseVo().getStore().getName());
            excel.setHouse(vo.getHouseVo().getName());

            String goods = "";
            BigDecimal waitNumber = new BigDecimal(0);
            for (HouseWaitOutGoodsVo hwgv : vo.getHouseWaitOutGoodsVoList()){
                waitNumber = waitNumber.add(hwgv.getWaitOutNumber());
                goods += hwgv.getGoodsVo().getName() + "、";
            }

            for (HouseWaitOutPacketVo hwgv : vo.getHouseWaitOutPacketVoList()){
                waitNumber = waitNumber.add(hwgv.getWaitOutNumber());
                goods += hwgv.getPacketName() + "、";
            }
            goods = goods.substring(0,goods.length()-1);
            excel.setGoods(goods);

            excel.setWaitNumber(PdosUtils.getNumber(waitNumber,getBusinessSet()));
            if (vo.getStatus() == 1){
                excel.setStatus("未出库");
            } else if (vo.getStatus() == 2){
                excel.setStatus("部分出库");
            } else if (vo.getStatus() == 3){
                excel.setStatus("全部出库");
            }
            list.add(excel);
        }
        return list;
    }

}
